﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DIProject.Data
{
    public class Users
    {
        /// <summary>
        /// создаем нового пользователя
        /// </summary>
        /// <param name="userName">логин</param>
        /// <param name="userPassword">пароль</param>
        /// <param name="firstName">имя</param>
        /// <param name="secondName">отчество</param>
        /// <param name="lastName">фамилия</param>
        /// <returns>идентификаторо вновь созданного пользователя</returns>
        public static Int32 CreateDIUser(string userName, string userPassword, string firstName, string secondName, string lastName)
        {
            /*
             @UserName nvarchar(50),
	@UserPassword nvarchar(50),
	@FirstName nvarchar(50),
	@SecondName nvarchar(50),
	@LastName nvarchar(50),
	@NewUserId int OUTPUT
             */
            string conString = Properties.Settings.Default.DIAptekaConnectionString;
            string QueryString = @"dbo.CreateDIUser";
            try
            {
                using (SqlConnection connection = new SqlConnection(conString))
                {
                    SqlCommand command = new SqlCommand(QueryString, connection);
                    command.CommandType = CommandType.StoredProcedure;
                    SqlParameter _userName = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
                    _userName.Value = userName;
                    command.Parameters.Add(_userName);
                    SqlParameter _userPassword = new SqlParameter("@UserPassword", SqlDbType.VarChar, 50);
                    _userPassword.Value = userPassword;
                    command.Parameters.Add(_userPassword);
                    SqlParameter _firstName = new SqlParameter("@FirstName", SqlDbType.VarChar, 50);
                    _firstName.Value = firstName;
                    command.Parameters.Add(_firstName);
                    SqlParameter _secondName = new SqlParameter("@SecondName", SqlDbType.VarChar, 50);
                    _secondName.Value = secondName;
                    command.Parameters.Add(_secondName);
                    SqlParameter _lastName = new SqlParameter("@LastName", SqlDbType.VarChar, 50);
                    _lastName.Value = lastName;
                    command.Parameters.Add(_lastName);
                    SqlParameter _newUserId = new SqlParameter("@NewUserId", SqlDbType.Int);
                    _newUserId.Direction = ParameterDirection.Output;
                    command.Parameters.Add(_newUserId);
                    connection.Open();
                    command.ExecuteNonQuery();
                    object obj = _newUserId.Value;
                    if (Convert.IsDBNull(obj))
                    {
                        throw new Util.DIException("Не удалось создать пользователя");
                    }
                    Int32 result = (Int32)obj;
                    return result;
                }
            }
            catch (SqlException se)
            {
                throw new Util.DIException("Не удалось создать пользователя", se);
            }
            catch (Exception ee)
            {
                throw new Util.DIException("Не удалось создать пользователя", ee);
            }
        }
    }
}
